﻿
    

    $(function () {
        _vmBase = new viewModel();

        _vmBase.init();


    })

    function viewModel() {
        var me = this;
        VueViewModelBase.call(me);
        viewModelExtend.call(me);

        me.orgTreeUrl = _getTreeDataUrl;
        me.tableListUrl = _getTableListUrl;
        me.addUrl = _updateModelUrl;
        me.delUrl = _deleteModelUrl;
        me.tabObj = $('#tb_users');

        //重写方法
        me.init = function () {

            me.bindTree(me.orgTreeUrl, "orgTree");

            me.initTableList();

            //添加监听对象
            me.addMonitor();

            me.bindUserOrg();
        }

        me.initTableList = function () {
            var options = {
                search: false,
                method: 'GET',
                showExport: true,
                exportDataType: "all",
                columns: [
                {
                   checkbox: true
                },
                {
                   field: 'dH_Account',
                   title: '用户名'
                },
                {
                   field: 'dH_NickName',
                   title: '昵称'
                },
                {
                    field: 'dH_IsDeveloper',
                    title: '是否是开发者',
                    formatter: function (value, row, index) {
                        
                        if (row.dH_IsDeveloper) {
                            return '<span class="label label-success">是</span>';
                        } else{
                            return "否";
                        }
                    }
                }, {
                   field: 'orgName',
                   title: '组织'
                }, {
                   field: 'dH_MobilePhone',
                   title: '联系方式'
                }, {
                    field: 'dH_Birthday',
                    title: '生日',
                    formatter: DateFormat
                }, {
                    field: 'id',
                    title: '状态',
                    formatter: function (value, row, index) {
                        var enabledLabel = '<span class="label label-success"><i class="fa fa-unlock"></i>' + _enabledStr + '</span>';
                        var disabledLabel = '<span class="label label-default"><i class="fa fa-lock"></i> ' + _disabledStr + '</span>';
                        if (row.dH_EnableMark && !row.dH_DeleteMark){
                            return enabledLabel;
                        } else if (!row.dH_EnableMark && !row.dH_DeleteMark) {
                            return disabledLabel;
                        }
                    }
                },
                {
                    field:'id',
                    title: '操作',
                    formatter: function (value, row, index) {

                        var enabledEle = '<a href="javascript:void(0)" onclick="_vmBase.onEnabledClick(this,' + index + ',\'' + row.id + '\',\'' + _enabledUserUrl + '\',\'' + _disabledUserUrl +'\',event)">' + _enabledStr + '</a>';
                        var disabledEle = '<a href="javascript:void(0)" onclick="_vmBase.onDisabledClick(this,' + index + ',\'' + row.id + '\',\'' + _enabledUserUrl + '\',\'' + _disabledUserUrl +'\',event)">' + _disabledStr + '</a>';

                        if (row.dH_EnableMark && !row.dH_DeleteMark) {
                            return disabledEle;
                        } else if (!row.dH_EnableMark && !row.dH_DeleteMark) {
                            return  enabledEle;
                        }

                    }
                }
                ]
            }
            me.initList(options);

        }

        
        me.save = function (callback) {
            if (!$('#form1').formValid()) {
                return false;
                callback(false);
            }
            
            var model = vueModel.$data.entityObj;
            model.OrgName = model.Organizations;

            //校验是否已存在userId
            $dh.get(_checkUserIdExistedUrl, { userId: model.DH_Account, id: model.Id }, function (result) {
                if (result.status=200){
                    if (result.data){

                        $dh.alert("账号已存在！");
                        
                    } else {
                        //执行成功
                        $dh.post(me.addUrl, model, function (result) {
                            if (result.Status == ResultStatus.OK) {

                                callback(result, true);

                            } else {
                                callback(result, false);

                            }
                        });

                    }
                } else {
                    $dh.alert("服务器异常！");
                }

            });

            
        }

    }
    function viewModelExtend() {
        var me = this;

        me.addMonitor = function (monitorObj) {
            var targetObj = vueModel.entityObj;

            if (monitorObj != null && monitorObj != undefined) {
                me.addMonitorObj(targetObj, monitorObj);
            } else {
                var addObj = {
                            Id:'',
                            DH_Account: '',
                            DH_Gender: true,
                            DH_RealName: '',
                            DH_NickName: '',
                            Birthday: '',
                            DH_MobilePhone: '',
                            DH_Email: '',
                            DH_WeChat: '',
                            DH_Signature: '',
                            Organizations: '',
                            OrganizationIds: '',
                            DH_EnableMark: true,
                            DH_DeleteMark: false,
                            DH_Organize: '',
                            DH_OrganizeId: '',
                            DH_Description: ''


                        };
                        me.addMonitorObj(targetObj, addObj);
                    }
        }

        me.bindUserOrg = function () {
            $("#Organizations").on("click", function () {
                _userOrgTree.reload();
            });
            _userOrgTree = new ParentTreeMultiple(me.orgTreeUrl, "Organizations", "OrganizationIds", "userOrg", vueModel.entityObj);

        }
        
    }

    function choseDate() {
        WdatePicker({
            dateFmt: 'yyyy-MM-dd',
            onpicked: function () {
                var _date = $('#userBirthday').val();
                vueModel.$set(vueModel.entityObj, 'Birthday', _date);
            }
        });

    }
    


